using System;
using System.Text;
using UnityEngine;

[Serializable]
public class Inventory_Item
{
    private string itemID;
    public int stackSize = 1;
    public int price { get; private set; }
    public int sellPrice { get; private set; }
    public ItemDataSO itemData;
    public ItemEffect_DataSO itemEffect;

    public ItemModifier[] modifiers { get; private set; }

    public Inventory_Item(ItemDataSO itemData)
    {
        this.itemData = itemData;
        //Guid.NewGuid() 生成一个新的全局唯一标识符
        itemID = itemData.itemName + " - " + Guid.NewGuid();
        //判断当前物品是否为可装备的物品，如果是，则将其修饰器数组转换成装备的修饰器数组
        modifiers = EquipmentData()?.itemModifiers;

        itemEffect = itemData.effect;

        price = itemData.price;
        sellPrice = Mathf.FloorToInt(price * .4f);
    }
    //判断当前物品UI堆叠时是否还能添加至同一个卡槽
    public bool CanAddStackSize() => stackSize < itemData.maxStackSize;

    public void AddStackSize() => stackSize++;

    public void ReduceStackSize() => stackSize--;

    //添加修饰器
    public void AddModifier(Entity_Stat playerStat)
    {
        if (modifiers == null)
            return;
        foreach (var modifier in modifiers)
        {
            Stat statToModify = playerStat.GetStatByType(modifier.statType);
            statToModify.AddModifier(itemID, modifier.value);
        }
    }

    //移除修饰器
    public void RemoveModifier(Entity_Stat playerStat)
    {
        if (modifiers == null)
            return;
        foreach (var modifier in modifiers)
        {
            Stat statToModify = playerStat.GetStatByType(modifier.statType);
            statToModify.RemoveModifier(itemID);
        }
    }
    //判断当前物品的数据种类
    private EquipmentDataSO EquipmentData()
    {
        if (itemData is EquipmentDataSO equipmentData)
            return equipmentData;
        return null;
    }

    public string GetDescription()
    {
        if (itemData.itemType == ItemType.Material)
            return "User for crafting";

        if (itemData.itemType == ItemType.Consumable)
            return itemData.effect.effectDescription;

        StringBuilder stringBuilder = new("");

        foreach (var mod in modifiers)
        {
            string modName = GetStatNameByType(mod.statType).ToString();
            string modValue = IspercentageStat(mod.statType) ? (mod.value * 100).ToString() + "%" : mod.value.ToString();
            stringBuilder.AppendLine(" + " + modValue + "  " + modName);
        }
        
        if (itemData.effect != null)
        {
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("Unique Effect: ");
            stringBuilder.AppendLine(itemData.effect.effectDescription);
        }
        return stringBuilder.ToString();
    }

    private string GetStatNameByType(StatType type)
    {
        return type switch
        {
            StatType.MaxHealth => "Max Health",
            StatType.HealthRegen => "Health Regeneration",
            StatType.Strength => "Strength",
            StatType.Agility => "Agility",
            StatType.Intelligence => "Intelligence",
            StatType.Vitality => "Vitality",
            StatType.AttackSpeed => "Attack Speed",
            StatType.Damage => "Damage",
            StatType.CritChance => "Critcal Chance",
            StatType.CritPower => "Critcal Power",
            StatType.ArmorReduction => "Armor Reduction",
            StatType.FireDamage => "Fire Damage",
            StatType.IceDamage => "Ice Damage",
            StatType.LightningDamage => "Lightning Damage",
            StatType.ElementalDamage=>"Elemental Damage",
            StatType.Armor => "Armor",
            StatType.Evasion => "Evasion",
            StatType.IceResistance => "Ice Resistance",
            StatType.FireResistance => "Fire Resistance",
            StatType.LightningResistance => "Lightning Resistance",
            _ => "UnKnown Stat",
        };
    }
    //判断属性是否需要乘100设置为百分制
    private bool IspercentageStat(StatType type)
    {
        return type switch
        {
            StatType.ArmorReduction or
            StatType.IceResistance or
            StatType.FireResistance or
            StatType.LightningResistance or
            StatType.Evasion or
            StatType.AttackSpeed => true,

            _ => false,
        };
    }

}
